<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>公司管理</title>
    <jsp:include page="../../head.jsp"/>
    <script type="text/javascript">

        function ajaxDataFilter(treeId, parentNode, responseData) {
            if (!responseData.depts)return null;
            return responseData.depts;
        }
        ;

        function zTreeOnClick(event, treeId, treeNode) {
            var src = "";
            if (treeNode.isParent) {
                src = "../base/company-mainDept?id=" + treeNode.id;
            } else {
                src = "../base/company-mainUser?id=" + treeNode.id;
            }
            $('#center', window.parent.document).attr("src", src);
        }

        var setting = {
            async: {
                enable: true,
                url: "company-listDept",
                type: "post",
                dataType: "json",
                autoParam: ["id=id", "name"],
                dataFilter: ajaxDataFilter
            },
            callback: {
                onClick: zTreeOnClick
            }
        };

        $(function () {
            initTree("company-listCompany", null);
        });

        /**初始化公司树*/
        function initTree(src, params) {
            $.post(src, params, function (value) {
//                if (!value.companys)return;
                $.fn.zTree.init($("#tree"), setting, value);
            });
        }

        $(function () {
            $("#mrdForm").mrdForm({
                                      title: "查询列表",// 表单标题
                                      columns: [{
                                          lwidth: 120,
                                          cwidth: 140,
                                          name: "name",
                                          label: "公司名称"
                                      }],
                                      buttons: {
                                          'custom': [
                                              {id: "addDept", value: "添加", click: add},
                                              {id: "modify", value: "编辑", click: edit}
                                          ],
                                          queryEvent: function () {
                                              var rules = "";// 定义rules
                                              $.each($("#" + $.mrdForm.formContainerId
                                                       + " td[id^='td_control_']"),
                                                     function (i, n) {
                                                         var control = $(n).children().first();
                                                         if (!$.mrdForm.isEmpty(control.val())) {
                                                             rules +=
                                                                     "{'field':'" + control.attr(
                                                                             'name')
                                                                     + "','op':'cn','data':'"
                                                                     + control.val() + "'},";// 组装rules
                                                         }
                                                     });
                                              // 如果没有查询条件，则返回
                                              if (!$.mrdForm.isEmpty(rules)) {
                                                  rules = rules.substring(0, rules.length - 1);
                                                  var filters = '{"groupOp":"AND","rules":[' + rules
                                                                + ']}';// 定义filters
                                                  initTree("company-listCompany",
                                                           {filters: filters});
                                              } else {
                                                  initTree("company-listCompany", null);
                                              }
                                          }
                                      }
                                  });
            $("#mrdForm").mrdForm("resize");
        });

        //添加
        var add = function () {
            parent.location.href = "company-manageCompany";
        };
        var del = function () {
        };
        //编辑
        var edit = function () {
            var nodes = $.fn.zTree.getZTreeObj("tree").getSelectedNodes();
            if (!nodes || nodes == null || nodes == "") {
                alert("请选择需要编辑的公司");
                return;
            }
            if (nodes.length == 1) {
                if(nodes[0].level!=0){
                    alert("请选择需要编辑的公司");
                    return;
                }
                parent.location.href = "company-manageCompany?id=" + nodes[0].id;
            }
        };
    </script>
</head>
<body class="ui-widget-content remove_border" style="height: 100%">
<div id="mrdForm"></div>
<ul id="tree" class="ztree" style="width:230px; overflow:auto;"></ul>
</body>
</html>
